//
//  HSPNewCarViewController.m
//  Icar
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 小龙侠. All rights reserved.
//

#import "HSPNewCarViewController.h"
#import "BaseHeader.h"
#import "AFNBlock.h"
#import "HSPWebViewController.h"
@interface HSPNewCarViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableViewNewCar;
@property (nonatomic,assign)BOOL isUp;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,strong)NSMutableArray *CarArray;
@property (strong, nonatomic) NSMutableURLRequest *request;
@property (strong, nonatomic) NSURLConnection *connection;
@end

@implementation HSPNewCarViewController
- (void)viewWillAppear:(BOOL)animated
{
    [_tableViewNewCar reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view setBackgroundColor: [LYYNightModeHandle shareNightModeHandle].nightModeColor];
    [self tableViewCar];
    self.CarArray = [[NSMutableArray alloc]init];
    [self AlbumaHeader];
    [self AlbumaFooter];

    
}
- (void)tableViewCar
{
    _tableViewNewCar = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ADJUSTHEIGHT * 520) style:UITableViewStylePlain];
    _tableViewNewCar.delegate = self;
    _tableViewNewCar.dataSource =self;
    [self.view addSubview:_tableViewNewCar];
}
#pragma 上拉刷新
- (void)AlbumaHeader
{
    __block HSPNewCarViewController *vc = self;
    [_tableViewNewCar addLegendHeaderWithRefreshingBlock:^{
        vc.page = 1;
        [vc APPadndatanumber :1];
        _isUp = YES;
    }];
    [_tableViewNewCar.header beginRefreshing];
}
#pragma 下拉加载
- (void)AlbumaFooter
{
    __block HSPNewCarViewController *vcc = self;
    [_tableViewNewCar addLegendFooterWithRefreshingBlock:^{
        vcc.page = vcc.page + 1;
        [vcc APPadndatanumber:vcc.page];
        _isUp = NO;
    }];
}
- (void)APPadndatanumber:(NSInteger)number
{
    NSString *documentString = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
    NSString *pathString = [documentString stringByAppendingString:@"NewCar.plist"];
    if ([Reachability isConnectedInternet] == YES) {
        [AFNBlock getDataWithString:[NSString stringWithFormat:@"http://mrobot.pcauto.com.cn/v2/cms/channels/2?pageNo=%ld&pageSize=20&v=4.0.0",_page] block:^(id back) {
            if (_isUp == YES) {
                [self.CarArray removeAllObjects];
            }
            for (NSDictionary *dica in [back objectForKey:@"data"]) {
                HSPNewCarModel *modelCar = [HSPNewCarModel modelWithDictionary:dica];
                [self.CarArray addObject:modelCar];
            }
            
            [self.tableViewNewCar reloadData];
            [self.tableViewNewCar.header endRefreshing];
            [self.tableViewNewCar.footer endRefreshing];
            [NSKeyedArchiver archiveRootObject:_CarArray toFile:pathString];
//            NSLog(@"NewCar沙盒路径%@", pathString);
        }];
    }
    if (ISCONENNCTED == NO) {
        if (_isUp == YES) {
            [_CarArray removeAllObjects];
        }
        //读取
        self.CarArray = [NSMutableArray arrayWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:pathString]];
        [_tableViewNewCar reloadData];
        [_tableViewNewCar.header endRefreshing];
        [_tableViewNewCar.footer endRefreshing];
    }
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.CarArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85 * ADJUSTHEIGHT;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HSPFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ReusableCell"];
    if (cell == nil) {
        cell = [[HSPFirstCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"ReusableCell"];
    }
    HSPNewCarModel *model = self.CarArray[indexPath.row];
    self.tableViewNewCar.scrollsToTop = YES;
    [cell getCarImage:model];
    cell.backgroundColor = [LYYNightModeHandle shareNightModeHandle].nightModeColor;
    cell.titleLabel.textColor = [LYYNightModeHandle shareNightModeHandle].wordColor;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HSPWebViewController *push = [[HSPWebViewController alloc]init];
    push.modelNewCar = [self.CarArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:push animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
